package com.czy.seed.mvc.home.controller;

import com.alibaba.fastjson.JSON;
import com.czy.seed.mvc.base.controller.BaseController;
import com.czy.seed.mvc.home.entity.Estimate;
import com.czy.seed.mvc.home.service.EstimateService;
import com.czy.seed.mvc.sys.entity.SysUser;
import com.czy.seed.mvc.sys.service.SysUserService;
import com.czy.seed.mvc.util.PrincipalUtil;
import com.czy.seed.mvc.util.Res;
import com.czy.seed.mybatis.base.QueryParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.czy.seed.mvc.util.PrincipalUtil;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: liuhainian
 * Created by liuyun  16:45 2017/12/27.
 */
//查询评论信息
@RestController
@RequestMapping("/home/estimate")
public class EstimateController extends BaseController<Estimate> {
    public static Logger logger = LoggerFactory.getLogger(CommodityController.class);

    @Autowired
    private EstimateService estimateService;

    @RequestMapping("/selectShop")
    public Res selectShop(){
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        try{
            QueryParams queryParams = new QueryParams(Estimate.class);
            QueryParams.Criteria criteria =queryParams.createCriteria();
            queryParams.orderBy("id").desc();
            criteria.andLessThan("createTime", new Date());
            List<Estimate> estimate = estimateService.selectListByParams(queryParams);
            pageInfo.put("estimates",estimate);
            logger.info("查询成功："+estimate.get(0).getUserName());
        }catch(Exception e){
            logger.error("查询评论信息失败");
        }
        return Res.ok(pageInfo);
    }
    @RequestMapping("/insertShop")
    public Res insertShop(HttpServletRequest request){
        String product = request.getParameter("sizeForm");
        Estimate estimate = JSON.parseObject(product, Estimate.class);
        estimate.setCreateTime(new Date());
        estimate.setUserName(PrincipalUtil.getLoginUser().getUsername());
        estimate.setUserId(PrincipalUtil.getLoginUser().getId());
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        try{
             estimateService.insert(estimate);
            pageInfo.put("flag","true");
            logger.info("评价成功");
        }catch(Exception e){
            logger.error("评价失败");
            e.printStackTrace();
        }
        return Res.ok(pageInfo);
    }
}
